package com.wbao.dianniu.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.data.HeadInfoData;
import com.wbao.dianniu.im.LoginSampleHelper;
import com.wbao.dianniu.listener.IAddFriendListener;
import com.wbao.dianniu.listener.IBlackAccountListener;
import com.wbao.dianniu.listener.IFollowListener;
import com.wbao.dianniu.listener.IFriendDelListener;
import com.wbao.dianniu.listener.IHeadInfoListener;
import com.wbao.dianniu.listener.IShildAccountListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AddFriendManger;
import com.wbao.dianniu.manager.BlackAccountManager;
import com.wbao.dianniu.manager.FollowManager;
import com.wbao.dianniu.manager.FriendDelManager;
import com.wbao.dianniu.manager.HeadInfoManager;
import com.wbao.dianniu.manager.ModifyRemarkHelper;
import com.wbao.dianniu.manager.ShareSDKManager;
import com.wbao.dianniu.manager.ShildAccountManager;
import com.wbao.dianniu.manager.ModifyRemarkHelper.IRemarkChanged;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 个人信息
 * 
 * @author yejianjun
 *
 */
public class HeadInfoActivity extends Activity implements OnClickListener,IHeadInfoListener,IAddFriendListener, IFollowListener,IBlackAccountListener,IShildAccountListener,IRemarkChanged,IFriendDelListener {
	public static final String INTENT_FRIENDID= "com.intent.friendId";
	
	private LinearLayout waittingLayout;
	private ProgressBar waittingProBar;
	private TextView waittingTV;
	private TextView titleTV;
	private ImageButton rightBtn;
	private ImageButton backBtn;
	private int friendId;
	private ImageView headPic;
	private TextView nameTV;
	private TextView labelTV;
	private TextView signatureTV;
	private Button followBtn;
	private Button addButton;
	private HeadInfoManager manager;
	private HeadInfoData resultData = new HeadInfoData();
	private AddFriendManger addFriendManager;
	private Dialog dialog;
	private Dialog deleteDialog;
	private int ShildStatus = 0;
	private ImageView sexImage;
	private String headUrl;
	private TextView tv_follow,tv_collect,tv_qa,tv_activity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Utils.setTranslucentStatus(this,true);
			SystemBarTintManager tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
			tintManager.setStatusBarTintResource(R.color.title_bg);// 通知栏所需颜色
		}
		setContentView(R.layout.activity_head_info);
		initWattingView();
		
		titleTV = (TextView) findViewById(R.id.title);
		titleTV.setText("查看资料");
		backBtn = (ImageButton) findViewById(R.id.back_button);
		backBtn.setOnClickListener(this);
		initSandian();
		initBaseView();
		
		Intent intent = getIntent();
		friendId = intent.getIntExtra(INTENT_FRIENDID, 0);
		requestData(GlobalContext.accountId,friendId);
		ModifyRemarkHelper.getInstance().addListener(this);
	}
	
	private void initWattingView(){
		waittingLayout = (LinearLayout) findViewById(R.id.waitting_layout);
		waittingProBar = (ProgressBar) findViewById(R.id.waitting_progress);
		waittingTV = (TextView) findViewById(R.id.waitting_text_desc);
	}
	
	private void initSandian(){
		rightBtn = (ImageButton) findViewById(R.id.right_imagebutton);
		rightBtn.setVisibility(View.INVISIBLE);
		rightBtn.setImageResource(R.drawable.sandian);
		rightBtn.setOnClickListener(this);
	}
	
	private void initBaseView(){
		headPic = (ImageView) findViewById(R.id.head_info_image);
		nameTV = (TextView) findViewById(R.id.head_info_name);
		sexImage = (ImageView) findViewById(R.id.head_info_sex);
		labelTV = (TextView) findViewById(R.id.head_info_label);
		signatureTV = (TextView) findViewById(R.id.head_info_signature);
		followBtn  = (Button) findViewById(R.id.head_info_follow);
		addButton  = (Button) findViewById(R.id.head_info_addFriend);
		tv_follow = (TextView) findViewById(R.id.head_follow);
		tv_collect = (TextView) findViewById(R.id.head_collect);
		tv_qa = (TextView) findViewById(R.id.head_qa_counts);
		tv_activity = (TextView) findViewById(R.id.head_activity);
		
		headPic.setOnClickListener(this);
		followBtn.setOnClickListener(this);
		addButton.setOnClickListener(this);
	}
	
	 // 请求数据
	private void requestData(int accountId,int friendAccountId){
		manager = new HeadInfoManager(this);
		manager.addHeadListener(this);
		manager.headinfo(accountId,friendAccountId);
	}
	
	private void requestAddFriend(int accountId,int friendAccountId){
		addFriendManager = new AddFriendManger(this);
		addFriendManager.addFriendListener(this);
		addFriendManager.addFriend(accountId, friendAccountId);
	}
	
	/**
	 * @param accountId	用户ID
	 * @param friendAccountId	关注的用户ID
	 */
	private void requestFollow(int accountId,int followAccountId){
		FollowManager manager = new FollowManager(this);
		manager.addFollowListener(this);
		manager.follow(accountId, followAccountId);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ModifyRemarkHelper.getInstance().removeListener(this);
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
		}
		if (null != deleteDialog && deleteDialog.isShowing()) {
			deleteDialog.dismiss();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_info_image:
			/** 点击放大图片 */
//			Intent intent = new Intent();
//			intent.putExtra(GlobalContext.BUNDLE_HEADPIC, headUrl);
//			intent.setClass(this, HeadInfoViewActivity.class);
//			this.startActivity(intent);
			ArrayList<String> urls2 = new ArrayList<>();
			urls2.add(GlobalContext.cdndownUrl + headUrl);
			Utils.imageBrower(HeadInfoActivity.this, 0, urls2);
			break;
		case R.id.back_button:
			finish();
			break;
		case R.id.head_info_follow:	//关注/取消关注
			if(friendId == GlobalContext.accountId){
				Notification.toast(this, "不能关注自己");
				return;
			}
			
			if(1 == resultData.getIsFollow()){
				followBtn.setText("关注");
				 resultData.setIsFollow(0) ;
			}else{
				followBtn.setText("取消关注");
				 resultData.setIsFollow(1) ;
			}
			requestFollow(GlobalContext.accountId, resultData.getId());
			break;
		case R.id.head_info_addFriend:	//加好友/私聊
			if(friendId == GlobalContext.accountId){
				Notification.toast(this, "不能加自己为好友");
				return;
			}
			if(1 == resultData.getIsFriend()){
				//跳转到聊天界面
				String appkey = getResources().getString(R.string.app_key);
				LoginSampleHelper.getInstance().openChat(this,GlobalContext.environment + String.valueOf(resultData.getId()), appkey);
			}else if (0 == resultData.getIsFriend()){
				//加好友 私聊跳转
				if(2 == resultData.getFriendStatus()){
					//跳转到聊天界面
					String appkey = getResources().getString(R.string.app_key);
					LoginSampleHelper.getInstance().openChat(this,GlobalContext.environment + String.valueOf(resultData.getId()), appkey);
				}else if(0 == resultData.getFriendStatus() || (3 == resultData.getFriendStatus()) || (4 == resultData.getFriendStatus() )){
					//不是好友，判断是否可以加好友
					if(1 == resultData.getBeFriend()){
						Notification.toast(this, "该用户不允许加好友");
						return ;
					}else{
						if (!GlobalContext.isPerfectData) {
							showAuthLevel("您还未完成认证，请先认证！");
						} else {
							requestAddFriend(GlobalContext.accountId,resultData.getId());
						}
					}
				}else  if(1 == resultData.getFriendStatus()){
				}
			}
			break;
		case R.id.right_imagebutton:	// 右三点
			//点击三点 弹出对话框 
			showDialog();
			break;

		default:
			break;
		}
	}
	
	/**
	 * 显示认证弹出框
	 */
	private void showAuthLevel(String msg) {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view
				.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				// 跳转到认证界面
				Intent intent = new Intent();
				intent.setClass(HeadInfoActivity.this, PerfectInfoActivity.class);
				startActivity(intent);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}

	/**
	 * 显示认证弹出框
	 */
	private void showDialog() {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.BOTTOM);
		View view = LayoutInflater.from(this).inflate(R.layout.bottom_dialog, null);
		Button btn_share = (Button) view.findViewById(R.id.bottom_share);
		Button btn_shield= (Button) view.findViewById(R.id.bottom_pb);
		Button btn_black = (Button) view.findViewById(R.id.bottom_lh);
		Button btn_report= (Button) view.findViewById(R.id.bottom_jb);
		Button btn_cancel= (Button) view.findViewById(R.id.bottom_cancel);
		Button modifyBtn  = (Button) view.findViewById(R.id.modify_beizu);
		Button deleteBtn  = (Button) view.findViewById(R.id.delete_account);
		// 不是好友隐藏修改备注和删除好友
		if(1 != resultData.getIsFriend()){
			modifyBtn.setVisibility(View.GONE);
			deleteBtn.setVisibility(View.GONE);
		}
		btn_black.setText("拉黑");
		if(1 == ShildStatus){
			btn_shield.setText("已屏蔽");
		}else{
			btn_shield.setText("屏蔽");
		}
		// 删除好友
		deleteBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDelete();
			}
		});
		// 分享
		btn_share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				// 获取某个用户的头像
				String content = "昵称：" + resultData.getRealName() + "\n" + "标签：" + resultData.getLabel() ;
				//调用分享模块
				ShareSDKManager.getInstance().showShare(HeadInfoActivity.this,content,2,GlobalContext.cdndownUrl + headUrl,friendId);
			}
		});
		
		//修改备注
		modifyBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				Intent modifyIntent = new Intent();
				modifyIntent.putExtra(GlobalContext.BUNDLE_ACCOUNTID, friendId);
				modifyIntent.putExtra(GlobalContext.BUNDLE_REALNAME, resultData.getRealName());
				modifyIntent.setClass(HeadInfoActivity.this, ModifyRemarkActivity.class);
				startActivity(modifyIntent);
			}
		});
		
		// 屏蔽
		btn_shield.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				//屏蔽
				ShildAccountManager manager = new ShildAccountManager(HeadInfoActivity.this);
				manager.addShildAccountListener(HeadInfoActivity.this);
				manager.shildAccount(GlobalContext.accountId , friendId);
			}
		});
		// 拉黑
		btn_black.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				BlackAccountManager manager = new BlackAccountManager(HeadInfoActivity.this);
				manager.addBlackAccountListener(HeadInfoActivity.this);
				manager.blackAccount(GlobalContext.accountId , friendId);
			}
		});
		// 举报
		btn_report.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				if(friendId == GlobalContext.accountId){
					Notification.toast(HeadInfoActivity.this, "自己不能举报自己");
					return;
				}
				Intent reportIntent = new Intent();
				//类型1 举报用户
				reportIntent.putExtra(GlobalContext.INTENT_REPORT_TYPE, 1);
				reportIntent.putExtra(GlobalContext.INTENT_REPORT_OBJECTID, friendId);
				reportIntent.setClass(HeadInfoActivity.this, ReportActivity.class);
				startActivity(reportIntent);
			}
		});
		btn_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
			}
		});
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
	}
	
	private void showDelete(){
		deleteDialog = new Dialog(this, R.style.custom_Dialog);
		Window win = deleteDialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view
				.findViewById(R.id.custom_msg_message);
		message.setText("确认删除好友吗");
		deleteDialog.setContentView(view);
		deleteDialog.setCancelable(true);
		deleteDialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != deleteDialog && deleteDialog.isShowing()) {
					deleteDialog.dismiss();
				}
				FriendDelManager manager = new FriendDelManager(HeadInfoActivity.this);
				manager.addFriendDelListener(HeadInfoActivity.this);
				manager.friendDel(GlobalContext.accountId, friendId);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != deleteDialog && deleteDialog.isShowing()) {
					deleteDialog.dismiss();
				}
			}
		});
	}
	
	@Override
	public void onHeadFailure(int errorCode, String errorMsg) {
		waittingLayout.setVisibility(View.VISIBLE);
		waittingProBar.setVisibility(View.GONE);
		waittingTV.setText("加载失败，请稍后再试...");
//		ErrorMsgHandler.getInstance().onErrorMsg(this, errorCode, errorMsg);
	}

	@Override
	public void onHeadSuccess(HeadInfoData data) {
		waittingLayout.setVisibility(View.GONE);
		rightBtn.setVisibility(View.VISIBLE);
		//收到数据 显示
		if(null !=data){
			resultData = data;
			headUrl = data.getHeadPic();
			PictureDownloadUtils.displayHeadImage(this,headPic, data.getHeadPic());
			nameTV.setText(data.getRealName() == null ? "":data.getRealName());
			labelTV.setText(data.getLabel() == null ? "":data.getLabel());
			signatureTV.setText(data.getDescribe() == null ? "签名：":  "签名：" + data.getDescribe());
			if(1 == data.getSex()){
				sexImage.setImageResource(R.drawable.boy_icon);
			}else{
				sexImage.setImageResource(R.drawable.girl_icon);
			}
			
			if(1 == data.getIsFriend()){
				addButton.setText("私聊");
				addButton.setClickable(true);
			}else if (0 == data.getIsFriend()){
				if(2 == data.getFriendStatus()){
					addButton.setText("私聊");
					addButton.setClickable(true);
				}else if(0 == data.getFriendStatus() || 3 == data.getFriendStatus() || 4 == data.getFriendStatus()){
					addButton.setText("+好友");
					addButton.setClickable(true);
				}else if(1 == data.getFriendStatus()){
					addButton.setText("已申请");
					addButton.setBackgroundColor(getResources().getColor(R.color.remark_gray_bg));
					addButton.setClickable(false);
				}
			}
			
			if(1 == data.getIsFollow()){
				followBtn.setText("取消关注");
			}else{
				followBtn.setText("关注");
			}
			tv_follow.setText(data.getFollowCount() + "");
			tv_collect.setText(data.getCollectCount() + "");
			tv_qa.setText(data.getQuestCount()+"");
			tv_activity.setText(data.getActivityCount() + "");
		}
		
	}

	@Override
	public void onAddFriendFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onAddFriendSuccess() {
		Notification.toast(this, "请求已发送");
		addButton.setText(getResources().getString(R.string.already_applay));
		addButton.setBackgroundColor(getResources().getColor(R.color.remark_gray_bg));
		addButton.setClickable(false);
//		resultData.setIsFriend(1);
		resultData.setFriendStatus(1);
	}

	@Override
	public void onFollowFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onFollowSuccess() {
	}

	@Override
	public void onBlackAccountFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onBlackAccountSuccess() {
		Notification.toast(this, "拉黑成功");
	}

	@Override
	public void onShildAccountFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onShildAccountSuccess() {
		if(0 == ShildStatus){
			ShildStatus = 1;
			Notification.toast(this, "已屏蔽");
		}else{
			ShildStatus = 0;
			Notification.toast(this, "已取消");
		}
	}

	@Override
	public void remarkChanged(String remark) {
		nameTV.setText(remark);
	}

	@Override
	public void onFriendDelFailure(int errorCode, String errorMsg) {
		Notification.toast(this, errorMsg);
	}

	@Override
	public void onFriendDelSuccess() {
		Notification.toast(this, "删除成功");
		finish();
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
